package com.huawei.spare.part.replace.views.widgets;

import com.google.common.collect.Lists;
import com.huawei.spare.part.replace.framework.SmartDataBus;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialog;
import com.jfoenix.controls.JFXDialogLayout;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

import java.util.List;

public class Models {

    /**
     * Show a overlay loading model
     *
     * @param root         stack-pane to hold the model
     * @param message      loading message
     * @param overlayClose closable when click on overlay part
     * @return model dialog object
     */
    public static JFXDialog loading(StackPane root, String message, boolean overlayClose) {
        VBox vbox = new VBox();
        vbox.setBackground(Background.EMPTY);
        vbox.setPadding(new Insets(32));
        vbox.setSpacing(16d);
        vbox.setAlignment(Pos.CENTER);

        final ProgressIndicator progressIndicator = new ProgressIndicator();
        progressIndicator.setPrefWidth(32d);
        progressIndicator.setPrefHeight(32d);
        progressIndicator.setMaxWidth(32d);
        progressIndicator.setMaxHeight(32d);
        vbox.getChildren().add(progressIndicator);

        if (message == null || message.length() == 0) {
            message = "Loading...";
        }
        Label messageLabel = new Label();
        messageLabel.setId("message");
        messageLabel.setStyle("-fx-font-size: 14;");
        messageLabel.setText(message);
        vbox.getChildren().add(messageLabel);

        JFXDialog dialog = new JFXDialog(root, vbox, JFXDialog.DialogTransition.TOP);
        dialog.setOverlayClose(overlayClose);
        return dialog;
    }

    public static JFXDialog alert(StackPane root, String title, String content) {
        return dialog(root, title, content, null);
    }

    public static JFXDialog dialog(StackPane root, String title, String content, List<JFXButton> buttons) {
        if (buttons == null) buttons = Lists.newArrayList();
        if (buttons.isEmpty()) {
            buttons.add(new JFXButton(SmartDataBus.getString("button.confirm")));
        }

        JFXDialogLayout layout = new JFXDialogLayout();
        JFXDialog dialog = new JFXDialog(root, layout, JFXDialog.DialogTransition.TOP);

        buttons.forEach(controlButton -> {
            controlButton.getStyleClass().add("dialog-button");
            controlButton.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent mouseEvent) -> {
                dialog.close();
            });
        });

        layout.setHeading(new Label(SmartDataBus.getString(title)));
        layout.setBody(new Label(SmartDataBus.getString(content)));
        layout.setActions(buttons);
        dialog.show();
        return dialog;
    }

}
